var fs = require('fs');
var mysql = require('mysql');
var superagent = require('superagent');
var cheerio = require('cheerio');

require('superagent-proxy')(superagent);

var mysqlConfig1 = require('./config').mysqlConfig1;
var mysqlConfig2 = require('./config').mysqlConfig2;

var ipList = [];
var ipIndex = 0;
var proxy = '';

var connection1 = mysql.createConnection(mysqlConfig2);
var query1 = 'select * from ip_table1';
connection1.connect();
connection1.query(query1, function (err, res) {
    if (err) {
        console.log('query1 error');

    } else {
        ipList = res;
        console.log(ipList.length);
        console.log(proxy);
        setTimeout(() => {
            changeIp();
        }, 300 * 1000);
        for (var i = 0; i < 20; i++) {
            var offset = 10 * i;
            fn1(offset);
        }
    }
});
connection1.end();


// https://music.163.com/#/discover/artist/cat?id=1001&initial=-1
function fn1(offset) {
    setProxy();
    console.log(proxy);

    let query1 = `SELECT	* FROM	song WHERE	ISNULL( comment_updated ) 	LIMIT 1 offset ${offset}`;
    var connection2 = mysql.createConnection(mysqlConfig1);
    connection2.query(query1, function (err, res) {
        console.log(res);
        if (res.length > 0) {
            let songId = res[0].id;
            let url = 'http://music.163.com/api/v1/resource/comments/R_SO_4_' + songId;

            superagent.get(url)
                .proxy(proxy)
                .end(function (err, res) {

                    if (err) {
                        console.log('http error');
                        changeIp();
                        fn1(offset);
                    } else {
                        // console.log(res);
                        // console.log(Object.keys(res));
                        // console.log(Object.keys(res.text));
                        var o = {};
                        try {
                            o = JSON.parse(res.text);
                        } catch (error) {
                            console.log(error);

                        }
                        // var o = JSON.parse(res.text);

                        console.log(o.hotComments);
                        if (o.hotComments) {

                            var list1 = o.hotComments.map(v => {
                                var id = v.commentId;
                                var content = v.content;
                                var time = v.time;
                                var likedCount = v.likedCount;
                                // var songId = songId;

                                return [id, content, time, likedCount, songId];
                            });
                            console.log('list1');
                            console.log(list1);

                            if (list1.length > 0) {
                                var connection3 = mysql.createConnection(mysqlConfig1);
                                var query3 = 'insert ignore into comment (id,content,time,liked_count,song_id) values ?';
                                connection3.connect();
                                connection3.query(query3, [list1], function (err, res) {
                                    if (err) {
                                        console.log('query3 err');
                                        console.log(err);

                                    } else {
                                        console.log(res);
                                        var connection4 = mysql.createConnection(mysqlConfig1);
                                        var query4 = 'update song set comment_updated=1 where id=' + songId;
                                        console.log(query4);
                                        connection4.connect();
                                        connection4.query(query4, function (err, res) {
                                            if (err) {
                                                // 
                                            } else {
                                                fn1(offset);
                                            }
                                        });
                                        connection4.end();
                                    }
                                });
                                connection3.end();
                            } else {
                                var connection4 = mysql.createConnection(mysqlConfig1);
                                var query4 = 'update song set comment_updated=1 where id=' + songId;
                                console.log(query4);
                                connection4.connect();
                                connection4.query(query4, function (err, res) {
                                    if (err) {
                                        // 
                                    } else {
                                        fn1(offset);
                                    }
                                });
                                connection4.end();
                            }
                        } else {
                            offset += 1;
                            fn1(offset);
                        }

                    }
                })
        }
    });
    connection2.end();
}

function updateStatus() {

}

function changeIp() {
    if (ipIndex < ipList.length) {
        ipIndex += 1;
    } else {
        ipIndex = 0;
    }
}

function setProxy() {
    var item = ipList[ipIndex];
    console.log(item);

    var ip = item.ip;
    var port = item.port;

    proxy = `http://${ip}:${port}`;
    console.log(proxy);

}